//
//  WaitView.m
//  DzNews
//
//  Created by zengyuan on 7/11/13.
//  Copyright (c) 2013 zengyuan. All rights reserved.
//

#import "WaitView.h"
#import "Config.h"
@implementation WaitView


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        // Initialization code
        self.circle=[[UIImageView alloc]initWithFrame:CGRectMake((self.frame.size.width-46)/2.0, 0, 46, 48)];
        [self addSubview:self.circle];
        self.circle.image=[UIImage imageNamed:@"loading"];
 
        
        self.tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.circle.frame.origin.y + self.circle.frame.size.height+3, self.frame.size.width, 20)];
        self.tipLabel.backgroundColor = [UIColor clearColor];
        self.tipLabel.font = [UIFont systemFontOfSize:16];
        self.tipLabel.textColor = [UIColor whiteColor];
        self.tipLabel.textAlignment = NSTextAlignmentCenter;
        self.tipLabel.text = @"加载中...";
        [self addSubview:self.tipLabel];
 
        angle=90;
    }
    return self;
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

-(void)rollAnimation{
    if(quit){
        self.isRolling=NO;
        return;
    }
    
    
//    if (self.isRolling) {
//        return;
//    }


    self.isRolling=YES;
    
    //参数为弧度制 M_PI代表180度
    CGAffineTransform endAngle = CGAffineTransformMakeRotation(M_PI*(angle/180.0));
    
    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.circle.transform = endAngle;
    } completion:^(BOOL finished) {
        
        // 旋转了360度
//        if (angle%360 == 0) {
            //退出不旋转了
//            if (quit) {
//                self.isRolling=NO;
//                quit=NO;
//                return ;
//            }
//        }
        angle+=90;

        [self rollAnimation];
    }];
}

//-(void)stopAnimation{
//    quit=YES;
//    self.isRolling=NO;
//}

-(void)startRoll{
    //旋转中
    if(self.isRolling) {
        return;
    }
    
    quit=NO;
    [self rollAnimation];
}

-(void)stopRoll{
    quit=YES;

}

@end


@implementation ErrorView

- (id)initWithFrame:(CGRect)frame text:(NSString *)text
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 34, 29)];
        self.imageView.image = [UIImage imageNamed:@"error"];
        [self addSubview:self.imageView];
        
        
        self.errLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.imageView.frame.origin.x+self.imageView.frame.size.width+2, 5, 10, 20)];
        self.errLabel.backgroundColor = [UIColor clearColor];
        self.errLabel.textColor = [UIColor whiteColor];
        self.errLabel.font = [UIFont systemFontOfSize:16];
        [self addSubview:self.errLabel];
        self.errLabel.text=text;
        [self.errLabel sizeToFit];

        CGRect rect=self.frame;
        rect.size.width=self.errLabel.frame.origin.x+self.errLabel.frame.size.width+2;
        self.frame=rect;
        
        self.btn =[UIButton buttonWithType:UIButtonTypeCustom];
        [self.btn setFrame:self.bounds];
        [self addSubview:self.btn];

    }
    
    return self;
}@end


@implementation WaitViewCannotRoll
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.circle.hidden=YES;

    }
    
    return self;
}
@end

@implementation WaitMutibleError
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.circle.hidden=YES;
        self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 300, 30)];
        self.titleLabel.backgroundColor = [UIColor clearColor];
        self.titleLabel.text =@"网络请求超时，请检查网络设置";
        self.titleLabel.textColor = RGB(155, 155, 155);
        self.titleLabel.numberOfLines = 0;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.titleLabel];
        self.titleLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(10, 30, 300, 30)];
        self.titleLabel2.backgroundColor = [UIColor clearColor];
        self.titleLabel2.text =@"下拉重新加载数据";
        self.titleLabel2.textColor = RGB(155, 155, 155);
        self.titleLabel2.numberOfLines = 0;
        self.titleLabel2.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.titleLabel2];
        
//        self.waitLabel.frame = CGRectMake((320-134)/2.0, self.circle.frame.origin.y+82, 134, 54);
    }
    
    return self;
}
@end

